package com.useeinfo.oa.modules.maintaining.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.maintaining.entity.PeriodInstall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yajuan.wang on 2018/10/9
 */
@Repository("periodInstallDaoWeb")
public class PeriodInstallDao implements CrudDao<PeriodInstall> {

    private final static Logger logger = LoggerFactory.getLogger(PeriodInstallDao.class);
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("deleteStatus = ?{paramIndex} ",false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        for(String queryKey : queryHash.keySet()){
            String queryValue = queryHash.get(queryKey);
            switch(queryKey){
                case "name":
                    conditionHash.put("name = ?{paramIndex}", queryValue);
                    continue;
                case "periodName":
                    conditionHash.put("(f.name like ?{paramIndex})", '%' + queryValue + '%');
                    continue;
                case "enterpriseId":
                    conditionHash.put("(f.enterprise.id = ?{paramIndex})", StringConverters.null2Long(queryValue));
                    continue;
            }

        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(f) from PeriodInstall f ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<PeriodInstall> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select f from  PeriodInstall f ", "order by f.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<PeriodInstall> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, PeriodInstall.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public PeriodInstall findModel(Long id) {
        return entityManager.find(PeriodInstall.class, id);
    }

    @Override
    public Integer add(PeriodInstall model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("PeriodInstallDaoImpl添加PeriodInstall成功！");
        return 1;
    }

    @Override
    public Integer update(PeriodInstall model) {
        PeriodInstall periodInstall = entityManager.find(PeriodInstall.class, model.getId());
        periodInstall.setPlanType(model.getPlanType());
        periodInstall.setRemark(model.getRemark());
        periodInstall.setName(model.getName());
        periodInstall.setTime(model.getTime());
        periodInstall.setWeek(model.getWeek());
        periodInstall.setFrequency(model.getFrequency());
        periodInstall.setType(model.getType());
        periodInstall.setAllDate(model.getAllDate());
        periodInstall.setUpdateDate(new Date());
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        PeriodInstall periodInstall = entityManager.find(PeriodInstall.class, id);
        entityManager.remove(periodInstall);
        return 1;
    }

    @Override
    public Integer deleteLogic(PeriodInstall model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
